import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Insets;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
/*
 * Created on Mar 24, 2008
 *
 */

/**
 * @author Anand
 */

public class JBackGroundImageDemo extends JFrame
{
    Container con = null;
    JPanel panelBgImg;
    
    
    public JBackGroundImageDemo()
    {
        setTitle("JBackGroundImageDemo");
        con = getContentPane();
        
        con.setLayout(null);
        ImageIcon imh = new ImageIcon(getClass().getResource("image.jpg"));
        
        
        setSize(imh.getIconWidth()+200, imh.getIconHeight()+200);
        
        
        
        panelBgImg = new JPanel()
        {
            public void paintComponent(Graphics g) 
            {
                Image img = new ImageIcon(getClass().getResource("image.jpg")).getImage();
                Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
                setPreferredSize(size);
                setMinimumSize(size);
                setMaximumSize(size);
                setSize(size);
                setBackground(Color.BLUE);
                setLayout(null);
                g.drawImage(img, 0, 0, null);
            } 
        };
        
        con.add(panelBgImg);
        panelBgImg.setBounds(100, 100, imh.getIconWidth(), imh.getIconHeight());
        
        GridBagLayout layout = new GridBagLayout();
        
        JPanel panelContent = new JPanel(layout);
        GridBagConstraints gc = new GridBagConstraints();

        gc.insets = new Insets(3, 3, 3, 3);
        gc.gridx = 1;
        gc.gridy = 1;
        
        JLabel label = new JLabel("UserName: ", JLabel.LEFT);                        
        panelContent.add(label, gc);
        
        gc.gridx = 2;
        gc.gridy = 1;
        
        JTextField txtName = new JTextField(10);
        panelContent.add(txtName, gc);
        
        gc.insets = new Insets(3, 3, 3, 3);
        gc.gridx = 1;
        gc.gridy = 2;
        gc.gridwidth = 2;

        JButton btn = new JButton("Login");
        panelContent.add(btn, gc);
        panelContent.setBackground(Color.RED);
        panelContent.setBorder(new LineBorder(Color.WHITE));
        
        JPanel panel = new JPanel();
        panel.add(new JButton("Press me"));
        
        panelBgImg.add(panelContent);
        
        
        panelBgImg.setLayout(new FlowLayout(FlowLayout.CENTER, 150, 200));
        
               
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
    public final static boolean RIGHT_TO_LEFT = false;

    public static void addComponentsToPane(Container contentPane) {
        if (RIGHT_TO_LEFT) {
            contentPane.setComponentOrientation(
                ComponentOrientation.RIGHT_TO_LEFT);
        }
//        Any number of rows and 2 columns
        contentPane.setLayout(new GridLayout(0,2));

        contentPane.add(new JLabel("JLabel 1"));
        contentPane.add(new JButton("JButton 2"));
        contentPane.add(new JCheckBox("JCheckBox 3"));
        contentPane.add(new JTextField("Long-Named JTextField 4"));
        contentPane.add(new JButton("JButton 5"));
        contentPane.setBackground(Color.BLUE);
        
    }

    private static void createAndShowGUI() {
        JFrame.setDefaultLookAndFeelDecorated(true);

        JFrame frame = new JFrame("GridLayout Source Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Set up the content pane and components in GridLayout
        addComponentsToPane(frame.getContentPane());

        frame.pack();
        frame.setVisible(true);
    }
    public static void main(String[] args) 
    {
    	
        new JBackGroundImageDemo().setVisible(true);
        
    }
}   
	